home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / Developer Documentation / Recipes, Tech Notes & Articles / Tech Notes / Utilities Documentation / RefCtCol < prev    next >
Encoding:
Text File  |  1995-11-07  |  7.1 KB  |  84 lines  |  [TEXT/ttxt]

  1. OpenDocâ„¢ Utilities Documentation
  2.  
  3.  
  4. ODRefCntCollection: ref-counted obj collection
  5. by David McCusker
  6.  
  7. © 1995  Apple Computer, Inc. All Rights Reserved.
  8. Apple, the Apple logo, and Macintosh are registered trademarks of Apple Computer, Inc.
  9. Mac and OpenDoc are trademarks of Apple Computer, Inc. 
  10.  
  11.  
  12. ODRefCntCollection is a utility providing a collection (ordered list) that contains instances of ODRefCntObject.  The interface is intentionally similar to another OpenDoc utility, OrderedCollection, which also provides the bulk of the implementation.  ODRefCntCollection is intended to be a replacement for OrderedCollection when the elements are reference counted.
  13.  
  14. There are two reasons why using ODRefCntCollection instead of OrderedCollection is a good idea. First, it is difficult for OpenDoc clients to ref-count objects correctly without taking a strictly formal approach to acquiring and releasing objects in a balanced fashion.  Using ODRefCntCollection, objects are acquired when the enter the collection and released if and only if elements are explicitly removed or the collection is destroyed.  (The RemoveFirst(),  RemoveLast(), and Remove()  methods are exceptions, in that they do not release, because presumably the client intends to use the object removed before releasing it.) This maintains the simple invariant that an object in the collection has one extra reference when it is in the collection.  The second reason is to reduce code size; calls to acquire or release occur inside the collection and not every place that objects are either added to or removed from the collection.
  15.  
  16. While OrderedCollection was designed to be subclassed, ODRefCntCollection was not. ODRefCntCollection uses a subclass of OrderedCollection named ODObjectOrdColl, which knows that reference counted objects must be compared for equality using ODObjectsAreEqual(), and overrides ODObjectOrdColl::ElementsMatch() to do this. ODRefCntCollection has (not is) an instance of ODObjectOrdColl, and many ODRefCntCollection methods are implemented as non-virtual inline calls to appropriate OrderedCollection methods.
  17.  
  18. The iterator for ODRefCntCollection is named ODRefCntCollectionIterator. The implementation of ODRefCntCollectionIterator delegates all operations to OrderedCollectionIterator.  Clients are expected to create instances of ODRefCntCollectionIterator on the stack, although nothing stops clients from creating them in the heap if desired.  However, note that there is no CreateIterator() method defined on the collection class encouraging clients to place iterators pointlessly in the heap.
  19.  
  20. ODRefCntCollection
  21.  
  22.  
  23. ODRefCntCollection::ODRefCntCollection(Environment* ev);
  24. ODRefCntCollection::ODRefCntCollection(Environment* ev, ODMemoryHeapID where);
  25. Both constructors save the environment for use in making reference count calls on objects in the collection, so methods that make such calls do not need an environment argument.  (The motivation was to support releasing in the destructor  which needs the environment but cannot pass one in.)  The collection allocates memory in the heap specified by where, or else in the default heap if not provided.
  26.  
  27. ODRefCntCollection::~ODRefCntCollection();
  28. Removes and releases every objects in the collection.
  29.  
  30. ODULong ODRefCntCollection::Count() const;
  31. Returns the number of objects in the collection.
  32.  
  33. void ODRefCntCollection::AddFirstAndAcquire(ODRefCntObject* element);
  34. Adds element to the front of the ordered collection, and then acquires element.
  35.  
  36. void ODRefCntCollection::AddLastAndAcquire(ODRefCntObject* element);  
  37. Adds element to the back of the ordered collection, and then acquires element.
  38.  
  39. void ODRefCntCollection::AddBeforeAndAcquire(
  40.                     ODRefCntObject* existing, 
  41.                     ODRefCntObject* tobeadded);
  42. Adds tobeadded in front of existing, and then acquires tobeadded.  Note that ODObjectsAreEqual() is used as the test for equality between existing and elements in the collection.
  43.  
  44. void ODRefCntCollection::AddAfterAndAcquire(
  45.                     ODRefCntObject* existing, 
  46.                     ODRefCntObject* tobeadded);
  47. Adds tobeadded in back of existing, and then acquires tobeadded.  Note that ODObjectsAreEqual() is used as the test for equality between existing and elements in the collection.
  48.  
  49. ODRefCntObject* ODRefCntCollection::After(ODRefCntObject* existing) const;
  50. Returns the object after existing in the collection, or null if there is no object after existing.  Note that ODObjectsAreEqual() is used as the test for equality between existing and elements in the collection.
  51.  
  52. ODRefCntObject* ODRefCntCollection::Before(ODRefCntObject* existing) const;
  53. Returns the object before existing in the collection, or null if there is no object before existing.  Note that ODObjectsAreEqual() is used as the test for equality between existing and elements in the collection.
  54.  
  55. ODRefCntObject* ODRefCntCollection::First() const;
  56. Returns the first object in the collection, or null if the collection is empty.
  57.  
  58. ODRefCntObject* ODRefCntCollection::Last() const;
  59. Returns the last object in the collection, or null if the collection is empty.
  60.  
  61. ODRefCntObject* ODRefCntCollection::RemoveFirst();
  62. Removes and returns the first object in the collection, or null if there are no objects in the collection.  An object so removed is not released, and clients must eventually release it.  The object is not released because presumably clients want to use it, because the first element in particular was requested.
  63.  
  64. ODRefCntObject* ODRefCntCollection::RemoveLast();  
  65. Removes and returns the last object in the collection, or null if there are no objects in the collection.  An object so removed is not released, and clients must eventually release it.  The object is not released because presumably clients want to use it, because the last element in particular was requested.
  66.  
  67. void ODRefCntCollection::RemoveAndReleaseAll();
  68. Removes and releases every object in the collection.
  69.  
  70. ODBoolean ODRefCntCollection::Remove(ODRefCntObject* existing); 
  71. Removes existing if it was in the collection, returning true if existing was in the collection or false if it was not  in the collection. existing is not released, and clients must eventually release existing if it was in the collection. (Use RemoveAndRelease() if you want it both removed and released.)  Note that ODObjectsAreEqual() is used as the test for equality between existing and elements in the collection.
  72.  
  73. ODBoolean ODRefCntCollection::RemoveAndRelease(ODRefCntObject* existing);
  74. Removes existing if it was in the collection, returning true if existing was in the collection or false if it was not  in the collection. 
  75. existing is released if and only if it was in the collection.  If the collection had the last reference to existing, then clients must not make any more calls on  existing after it is released.  Note that ODObjectsAreEqual() is used as the test for equality between existing and elements in the collection.
  76.  
  77. ODBoolean ODRefCntCollection::Contains(ODRefCntObject* existing) const;
  78. Returns whether existing is currently in the collection.  Note that ODObjectsAreEqual() is used as the test for equality between existing and elements in the collection.
  79.  
  80. ODMemoryHeapID GetHeap() const;
  81. Return the heap in which the collection allocates memory.
  82.  
  83. <end>
  84.